package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.UserOperationsAdapter;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.bean.UserDynamicBean;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils2;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class UserOperationsActivity extends BaseActivity {
    private ImageView icon_back;
    private TextView tx_title;
    private int userid;
    private String token;
    private RecyclerView rc_op;
    private UserOperationsAdapter adapter;
    @Override
    protected void initListener() {
        icon_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_useroperations);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        getRequest();
        icon_back = findViewById(R.id.icon_back);
        tx_title = findViewById(R.id.tx_title);
        rc_op = findViewById(R.id.rc_op);
        Intent intent = getIntent();
        userid = intent.getIntExtra("userid", 0);
        String name = intent.getStringExtra("name");
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        tx_title.setText(name+getString(R.string.mi_dedongtai));
        getUserDynamic(token,EXTRA,userid);
    }
    private void getUserDynamic(String token, String extra, int userid) {
        NetUtils2.getInstance().getApiService().getUserDynamic(token, extra, userid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("decryptStr23", decryptStr);
                        Gson gson = new Gson();
                        UserDynamicBean bean = gson.fromJson(decryptStr, UserDynamicBean.class);
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(UserOperationsActivity.this);
                        rc_op.setLayoutManager(linearLayoutManager);
                        if (bean != null) {
                            if (bean.getData() != null) {
                                if (bean.getData().getDynamics() != null && bean.getData().getDynamics().size() != 0) {
                                    adapter = new UserOperationsAdapter(bean.getData().getDynamics());
                                    rc_op.setAdapter(adapter);
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
